package com.xsq.page;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.xsq.Constants;
import com.xsq.R;
import com.xsq.permission.PermissionHelper;
import com.xsq.permission.PermissionInterface;
import com.xsq.view.MyEditDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.x;

@ContentView(R.layout.activity_splash)
public class SplashActivity extends BaseActivity implements PermissionInterface {

    private MyCountDownTimer mc;

    private PermissionHelper permissionHelper;


    @Override
    public int getPermissionsRequestCode() {
        return 1000;
    }

    @Override
    public String[] getPermissions() {
        return new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        };
    }

    @Override
    public void requestPermissionsSuccess() {
        //双屏需要的权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                try {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                gotoMain();
            }
        } else {
            gotoMain();
        }
    }

    @Override
    public void requestPermissionFail() {
        this.finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (permissionHelper.requestPermissionsResult(requestCode, permissions, grantResults)) {
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        x.view().inject(this);
        mc = new MyCountDownTimer(1000, 1000);
        mc.start();
    }

    private Handler handler = new Handler();
    /**
     * 继承 CountDownTimer 防范
     *
     * 重写 父类的方法 onTick() 、 onFinish()
     */
    class MyCountDownTimer extends CountDownTimer {
        /**
         *
         * @param millisInFuture
         * 表示以毫秒为单位 倒计时的总数
         *
         * 例如 millisInFuture=1000 表示1秒
         *
         * @param countDownInterval
         * 表示 间隔 多少微秒 调用一次 onTick 方法
         *
         * 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         *
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        public void onFinish() {
            //tv.setText("0s");
            gotoLogin();
        }
        public void onTick(long millisUntilFinished) {
            //tv.setText("" + millisUntilFinished / 1000 + "s");
        }
    }

    private void gotoLogin() {
        /*handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                *//*Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();*//*
                if(BaseApplication.getInstance().hasConfiged()) {
                    gotoMain();
                }else {
                    showEditDialog();
                }
            }
        }, 200);*/

        permissionHelper = new PermissionHelper(this, this);
        permissionHelper.requestPermissions();
    }

    private void showEditDialog() {
        final MyEditDialog editDialog = new MyEditDialog(this, 20, "请输入密码", "", "取消", "确定");
        editDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editDialog.dismiss();
                finish();
            }
        });
        editDialog.setOkListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = editDialog.getEditView().getText().toString().trim();
                if(Constants.AdminPwd.equals(password)) {
                    editDialog.dismiss();
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(SplashActivity.this, "密码输入错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        editDialog.show();
    }

    private void gotoMain() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10) {
            if (Settings.canDrawOverlays(this)) {
                gotoMain();
            }
        }
    }

}
